/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import workdb.database;
/**
 *
 * @author Андрей
 */
public class MyServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /*
             * TODO output your page here. You may use following sample code.
             */
            String action=request.getParameter("action");
            if(action.equals("del")){
                try {
                    database.delEmp(request.getParameter("id"));
                    out.println("<html>");
                    out.println("<head>");
                    out.println("<title>Удаление</title>");            
                    out.println("</head>");
                    out.println("<body bgcolor='#0066FF'>");
                    out.println("<h1>Сотрудник удален.</h1>");
                    out.println("<p><input type='button' value='На главную' name='B2' onclick=location.href='/employee'></p>");
                    out.println("</body>");
                    out.println("</html>");
                } catch (SQLException ex) {
                    response.sendError(400);
                }
            }
            else{
                if(action.equals("find")){
                    try {
                        String [] res=database.findEmp(request.getParameter("id"));
                        if(res[0]!=null){
                            out.println("<html>");
                            out.println("<head>");
                            out.println("<title>Редактирование</title>");            
                            out.println("</head>");
                            out.println("<body bgcolor='#0066FF'>");
                            out.println("<form name='f1' method='post' action='removeServlet?action=edit'>");
                            out.println("<fieldset style='padding: 2'>");
                            out.println("<legend>Редактирование</legend>");
                            out.println("Номер:<br>");
                             out.println("<input type='text' name='id' value="+request.getParameter("id")+"  readonly='readonly' size='20'><br>");
                            out.println("Имя:<br>");
                            out.println("<input type='text' name='ename' value="+res[0]+" size='20'><br>");
                            out.println("Должность:<br>");
                            out.println("<input type='text' name='job' value="+res[1]+" size='20'><br>");
                            out.println("Менеджер (оставить пустым если нет менеджера):<br>");
                            if(res[2]==null){
                                out.println("<input type='text' name='mgr' value='' size='20'><br>");
                            }
                            else{
                                out.println("<input type='text' name='mgr' value="+res[2]+" size='20'><br>");
                            }
                            out.println("Дата приема (dd-mm-yy):<br>");
                            out.println("<input type='text' name='hiredate' value="+res[3]+" size='20'><br>");
                            out.println("Зарплата:<br>");
                            out.println("<input type='text' name='sal' value="+res[4]+" size='20'><br>");
                            out.println("Коммисионные (оставить пустым если нет):<br>");
                             if(res[5]==null){
                                out.println("<input type='text' name='comm' value='' size='20'><br>");
                            }
                            else{
                                out.println("<input type='text' name='comm' value="+res[5]+" size='20'><br>");
                            }
                            out.println("Департамент:<br>");
                            out.println("<input type='text' name='deptno' value="+res[6]+" size='20'><br>");
                            out.println("</fieldset>");
                            out.println("<p><input type='submit' value='Сохранить' name='B1'></p>");
                            out.println("<p><input type='button' value='На главную' name='B2' onclick=location.href='/employee'></p>");
                            out.println("</body>");
                            out.println("</html>");
                        }
                        else{
                            out.println("<html>");
                            out.println("<head>");
                            out.println("<title>Редактирование</title>");            
                            out.println("</head>");
                            out.println("<body bgcolor='#0066FF'>");
                            out.println("<h1>Сотрудник не найден.</h1>");
                            out.println("<p><input type='button' value='На главную' name='B2' onclick=location.href='/employee'></p>");
                            out.println("</body>");
                            out.println("</html>"); 
                        }
                    } catch (SQLException ex) {
                       response.sendError(400);
                    }
                }
                if(action.equals("edit")){
                    try {
                        database.editEmp(request.getParameter("id"), request.getParameter("ename"), request.getParameter("job"), request.getParameter("mgr"), request.getParameter("hiredate"), request.getParameter("sal"), request.getParameter("comm"), request.getParameter("deptno"));
                        out.println("<html>");
                        out.println("<head>");
                        out.println("<title>Редактирование</title>");            
                        out.println("</head>");
                        out.println("<body bgcolor='#0066FF'>");
                        out.println("<h1>Изменения сохранены.</h1>");
                        out.println("<p><input type='button' value='На главную' name='B2' onclick=location.href='/employee'></p>");
                        out.println("</body>");
                        out.println("</html>"); 
                    } catch (SQLException ex) {
                        response.sendError(400);
                    }
                }
                else{
                    if(action.equals("new")){
                        try {
                            database.newEmp(request.getParameter("id"), request.getParameter("ename"), request.getParameter("job"), request.getParameter("mgr"), request.getParameter("hiredate"), request.getParameter("sal"), request.getParameter("comm"), request.getParameter("deptno"));
                            out.println("<html>");
                            out.println("<head>");
                            out.println("<title>Новый сотрудник</title>");            
                            out.println("</head>");
                            out.println("<body bgcolor='#0066FF'>");
                            out.println("<h1>Сотрудник добавлен.</h1>");
                            out.println("<p><input type='button' value='На главную' name='B2' onclick=location.href='/employee'></p>");
                            out.println("</body>");
                            out.println("</html>");
                        } catch (SQLException ex) {
                            response.sendError(400);
                        }
                    }
                }
            }
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
